var entries = Array(4);
var blockInsert = false;
var numberOfBoxes =0;

function insertTextField(td,name,index) { // td = table data
   var text = td.innerHTML;// text speichern für Textfeld
  if(text.indexOf('<input name=')===-1 && blockInsert === false) //if contains substring
  {
    entries[index]=text;
    td.innerHTML = ""; // Inhalt des <td>-Tags löschen
    var inner = "";
    if(name === "password")
    {
      inner = '<input name="password1" type="password" value="" /><br/>';
      inner += '<input name="password2" type="password" value="" />';
    }
    else
    {
      inner = '<input name="'+name+'" type="text" value="' + text + '" />';
    }
    inner+='<input type="button" value="Abbruch" name="abort" onclick="DeleteTextField(this.parentNode,'+index+')" />';
    numberOfBoxes++;
    var submitPar = document.getElementById("submitArea");
    submitPar.innerHTML='<input type="submit" value="Übernehmen" name="submit" />';
    td.innerHTML = inner;
  }
  else
    blockInsert=false;
}

function CreateList(td,name,index)
{
  var text = td.innerHTML;// text speichern für Textfeld
  if(text.indexOf('<select')===-1 && blockInsert === false) //if contains substring
  {
    entries[index]=text;
    td.innerHTML="";
    var box = '<select name ="'+name+'">';
        box+= '<option value="1">User</option>';
        box+= '<option value="2">Administrator</option>';
        box+= '</select>';
    box+='<input type="button" value="Abbruch" name="abort" onclick="DeleteTextField(this.parentNode,'+index+')" />';
    numberOfBoxes++;
    var submitPar = document.getElementById("submitArea");
    submitPar.innerHTML='<input type="submit" value="Übernehmen" name="submit" />';
    td.innerHTML = box;
  }
  else
    blockInsert=false;
}

function DeleteTextField(td,index)
{
  numberOfBoxes--;
  if(numberOfBoxes === 0)
  {
    var submitPar = document.getElementById("submitArea");
    submitPar.innerHTML="";
  }
  td.innerHTML=entries[index];
  blockInsert=true;
}

function addNewUser(p)
{
var form='<form name = "new" onsubmit="crpytPw()" action = "users.php" method = "POST">';
form+='<table>';
form+='<tr><td>User:</td><td><input type = "text" name = "name" value = "" /></td></tr>';
form+='<tr><td>Passwort:</td><td><input type = "password" name = "password1" value = "" /><br><input type = "password" name = "password2" value = "" /></td></tr>';
form+='<tr><td>Rechte:</td><td><select name ="rights">';
form+='<option value="1">User</option>';
form+='<option value="2">Administrator</option>';
form+='</select>';
form+='<tr><td>E-Mail:</td><td><input type = "text" name = "email" value = "" /></td></tr>';
form+='</form>';
form+='</table ><br>';
form+='<input type = "submit" value = "Übernehmen" name = "submit" />   ';
form+='<input type="button" value="Abbrechen" name="abort" onclick="removeNewUser(this.parentNode)"/>';
p.innerHTML=form;
}

function removeNewUser(p,id)
{
  p.innerHTML='<input type="button" value="neuer User" name="new" onclick="addNewUser(this.parentNode)"/>';
}

function cryptPw()
{
  
}

function deleteUser(td,id)
{
  if(confirm("User wirklich löschen?"))
  {
    
   //$.post("removeUser.php",{"id":id},function(){
   //   $(td).closest('tr').fadeTo(400,0,function(){$(this).remove();});
   $.post("ajax/removeUser.php",{"id":id}).done(function(data)
   {
     if(data==="true")
     {$(td).closest('tr').fadeTo(400,0,function(){$(this).remove();});}
   });   
   //});
  }
}

